package com.offcn.service.Impl;

import com.alibaba.dubbo.config.annotation.Service;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.offcn.mapper.CheckgroupCheckitemMapper;
import com.offcn.mapper.CheckgroupMapper;
import com.offcn.mapper.SetmealCheckgroupMapper;
import com.offcn.pojo.Checkgroup;
import com.offcn.pojo.CheckgroupCheckitem;
import com.offcn.pojo.SetmealCheckgroup;
import com.offcn.service.CheckgroupService;
import com.offcn.utils.PageResult;
import com.offcn.utils.QueryPageBean;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;
import java.util.stream.Collectors;

/**
 * @Author: wy
 * @Date: 2022/8/30 14:09
 */
@Component
@Service
public class CheckgroupServiceImpl implements CheckgroupService {

    @Autowired
    private CheckgroupMapper checkgroupMapper;

    @Autowired
    private CheckgroupCheckitemMapper checkgroupCheckitemMapper;


    @Autowired
    private SetmealCheckgroupMapper setmealCheckgroupMapper;

    //添加检查组

    @Transactional
    public void addGroup(Checkgroup checkgroup, Integer[] checkitemIds) {
        //先添加检查组
        checkgroupMapper.insert(checkgroup);

        //在添加组对用的检查项
        if (checkitemIds != null) {
            CheckgroupCheckitem checkgroupCheckitem = new CheckgroupCheckitem();

            checkgroupCheckitem.setCheckgroupId(checkgroup.getId());

            for (Integer checkitemId : checkitemIds) {
                checkgroupCheckitem.setCheckitemId(checkitemId);

                checkgroupCheckitemMapper.insert(checkgroupCheckitem);
            }

        }
    }

    //分页查询
    @Override
    public PageResult findPage(QueryPageBean queryPageBean) {
        Page<Checkgroup> page =
                new Page<>(queryPageBean.getCurrentPage(), queryPageBean.getPageSize());

        LambdaQueryWrapper<Checkgroup> lambdaQueryWrapper = new LambdaQueryWrapper<>();

        Page<Checkgroup> checkgroupPage = null;

        if (queryPageBean.getQueryString() != null && queryPageBean.getQueryString() != "") {
            lambdaQueryWrapper.like(Checkgroup::getCode, queryPageBean.getQueryString());
            lambdaQueryWrapper.or();
            lambdaQueryWrapper.like(Checkgroup::getName, queryPageBean.getQueryString());
            lambdaQueryWrapper.or();
            lambdaQueryWrapper.like(Checkgroup::getHelpcode, queryPageBean.getQueryString());
            checkgroupPage = checkgroupMapper.selectPage(page, lambdaQueryWrapper);
        } else {
            checkgroupPage = checkgroupMapper.selectPage(page, lambdaQueryWrapper);
        }

        PageResult checkgroupPageResult =
                new PageResult(checkgroupPage.getTotal(), checkgroupPage.getRecords());
        return checkgroupPageResult;
    }

    //修改检查组的数据
    @Override
    @Transactional
    public void editGroup(Checkgroup checkgroup, Integer[] checkitemIds) {
        //先进行修改检查组
        checkgroupMapper.updateById(checkgroup);

        //修改检查组下的检查项 id
        //第一步：先吧改修改的检查组下原有的 检查项id 删除 随后 在进行添加
        LambdaQueryWrapper<CheckgroupCheckitem> lambdaQueryWrapper = new LambdaQueryWrapper<>();
        lambdaQueryWrapper.eq(CheckgroupCheckitem::getCheckgroupId, checkgroup.getId());
        checkgroupCheckitemMapper.delete(lambdaQueryWrapper);

        //删除完成值 我们从新添加就可以
        //第二步： 在添加组对用的检查项
        if (checkitemIds != null) {
            CheckgroupCheckitem checkgroupCheckitem = new CheckgroupCheckitem();

            checkgroupCheckitem.setCheckgroupId(checkgroup.getId());

            for (Integer checkitemId : checkitemIds) {

                checkgroupCheckitem.setCheckitemId(checkitemId);

                checkgroupCheckitemMapper.insert(checkgroupCheckitem);
            }
        }
    }

    //删除
    @Override
    @Transactional
    public void deleteInfoByid(Integer id) {
        //第一步 先删除  CheckgroupCheckitem 里面的 Checkgroup_id
        LambdaQueryWrapper<CheckgroupCheckitem> checkitemLambdaQueryWrapper
                = new LambdaQueryWrapper<>();
        checkitemLambdaQueryWrapper.eq(CheckgroupCheckitem::getCheckgroupId, id);
        checkgroupCheckitemMapper.delete(checkitemLambdaQueryWrapper);
        //第二步 在删除  SetmealCheckgroup 里面的 Checkgroup_id
        LambdaQueryWrapper<SetmealCheckgroup> setmealCheckgroupLambdaQueryWrapper
                = new LambdaQueryWrapper<SetmealCheckgroup>();
        setmealCheckgroupLambdaQueryWrapper.eq(SetmealCheckgroup::getCheckgroupId, id);
        setmealCheckgroupMapper.delete(setmealCheckgroupLambdaQueryWrapper);
        //第三步 在删除 Checkgroup里面的 Checkgroup_id
        checkgroupMapper.deleteById(id);
    }

    //查询所有的检查组信息
    @Override
    public List<Checkgroup> showAllGroupInfo() {

        return checkgroupMapper.selectList(null);
    }

    @Override
    public List<Integer> getGroupIdBySetmealId(Integer id) {
        LambdaQueryWrapper<SetmealCheckgroup> lambdaQueryWrapper = new LambdaQueryWrapper<>();

        //根据 getSetmealId 进行查询 选中那个
        lambdaQueryWrapper.eq(SetmealCheckgroup::getSetmealId,id);
        List<SetmealCheckgroup> setmealCheckgroups = setmealCheckgroupMapper.selectList(lambdaQueryWrapper);

        List<Integer> collect = setmealCheckgroups.stream().
                map(setmealCheckgroup -> setmealCheckgroup.getCheckgroupId()).collect(Collectors.toList());
        return collect;
    }
}
